package internal

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"github.com/xuri/excelize/v2"
	"os"
	"server/libs/log"
	"strconv"
)

func Load_Language() {

	confg_lang := os.Getenv("SERVER_CONFIG_LANG_PATH")
	if confg_lang == "" {
		confg_lang = "./game_trans.xlsx"
	}

	//dataBytes, err := os.ReadFile(confg_lang)
	//if err != nil {
	//	log.Logrus().WithFields(logrus.Fields{}).Panic("trans file error")
	//	return
	//}
	//TotalLangMap = make(map[string]map[string]map[int64]string)
	//err = yaml.Unmarshal(dataBytes, &TotalLangMap)
	//if err != nil {
	//	log.Logrus().WithFields(logrus.Fields{}).Panic("trans file error")
	//	return
	//}
	LoadGameConfig()
}
func LoadGameConfig() {
	// 打开Excel文件
	f, err := excelize.OpenFile("./game_trans.xlsx")
	defer f.Close()
	if err != nil {
		fmt.Println("无法打开文件:", err)
		log.Logrus().WithFields(logrus.Fields{}).Panic("读取不到配置文件 game_trans.xlsx")
		return
	}

	// 获取工作表中的数据
	rows, err := f.GetRows("game") // Sheet1是工作表的名称，你可以根据实际情况修改
	if err != nil {
		fmt.Println("无法获取工作表数据:", err)
		return
	}
	TotalLangMap = make(map[string]map[string]map[int64]string)

	TotalLangMap["zh"] = make(map[string]map[int64]string)
	TotalLangMap["en"] = make(map[string]map[int64]string)
	TotalLangMap["fr"] = make(map[string]map[int64]string)
	TotalLangMap["es"] = make(map[string]map[int64]string)
	TotalLangMap["pt"] = make(map[string]map[int64]string)
	// 遍历每一行数据并打印
	for index, row := range rows {
		if index == 0 {
			continue
		}
		id, err := strconv.ParseInt(row[1], 10, 64)
		if err != nil {
			return
		}
		_, ok := TotalLangMap["zh"][row[0]]
		if !ok {
			TotalLangMap["zh"][row[0]] = make(map[int64]string)
			TotalLangMap["zh"][row[0]][id] = row[2]
		} else {
			TotalLangMap["zh"][row[0]][id] = row[2]
		}

		_, ok = TotalLangMap["en"][row[0]]
		if !ok {
			TotalLangMap["en"][row[0]] = make(map[int64]string)
			TotalLangMap["en"][row[0]][id] = row[3]
		} else {
			TotalLangMap["en"][row[0]][id] = row[3]
		}

		_, ok = TotalLangMap["fr"][row[0]]
		if !ok {
			TotalLangMap["fr"][row[0]] = make(map[int64]string)
			TotalLangMap["fr"][row[0]][id] = row[4]
		} else {
			TotalLangMap["fr"][row[0]][id] = row[4]
		}

		_, ok = TotalLangMap["es"][row[0]]
		if !ok {
			TotalLangMap["es"][row[0]] = make(map[int64]string)
			TotalLangMap["es"][row[0]][id] = row[5]
		} else {
			TotalLangMap["es"][row[0]][id] = row[5]
		}

		_, ok = TotalLangMap["pt"][row[0]]
		if !ok {
			TotalLangMap["pt"][row[0]] = make(map[int64]string)
			TotalLangMap["pt"][row[0]][id] = row[6]
		} else {
			TotalLangMap["pt"][row[0]][id] = row[6]
		}
	}
}
